package network;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import signals.*;


public class UDPSender {

	DatagramSocket socketL;
	DatagramSocket socketD;
	DatagramPacket packet;
	int portDest;
	InetAddress addrDest;
	byte[] buf = new byte [256];
	ChatNI netwInt;
	
	public UDPSender ( InetAddress addrD, ChatNI father,int pUDP) {
		netwInt = father;
		this.portDest = pUDP;
		this.addrDest = addrD;
		try {
		  
	      socketL = new DatagramSocket();
		  
		} catch (SocketException e){
			System.out.println("Error creating UDP socket to send");
			e.printStackTrace();
		}
		
	}
	
	public void send (Signal s) {
		buf = s.serialize();
		
		packet = new DatagramPacket(buf,buf.length,addrDest,portDest);
		try {
			socketL.send(packet);
		} catch (IOException e) {
			System.out.println("Error sending packet");
		}
	}
	
}
